package de.vogella.jface.treeviewer.provider;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import de.vogella.jface.treeviewer.model.Todo;
import de.vogella.jface.treeviewer.model.TodoMockModel;

public class TodoContentProvider implements ITreeContentProvider {

	private TodoMockModel model;

	
	public void dispose() {
	}

	
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		this.model = (TodoMockModel) newInput;
	}

	
	public Object[] getElements(Object inputElement) {
		if (model.getCategories() != null) {
			return model.getCategories().toArray();
		}
		return new Object[]{};
	}

	
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof Todo) {
			Todo category = (Todo) parentElement;
			if (category.getTodos().size() > 0) {
				return category.getTodos().toArray();
			}
		}
		return null;
	}

	
	public Object getParent(Object element) {
		return null;
	}

	
	public boolean hasChildren(Object element) {
		if (element instanceof Todo) {
			Todo category = (Todo) element;
			if (category.getTodos().size() > 0) {
				return true;
			}
		}
		return false;
	}

}